1.创建一个用户类,来接收发送邮件的邮箱地址和授权密码(需要注意的是,这里的密码不是正常使用邮箱的登陆密码,而是客户端生成的另一个专门的授权码 ,这里需要开启相应的发送邮件的服务,这里开启的是pop3/smtp服务)
package appsoft.cd.applications.cd_gcxm.web;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
class MailAuthenticator extends Authenticator {
/** 用户名 */
private String username;
/** 密码 */
private String password;
public MailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public String getPassword() {
return password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
2.编写相应的测试方法(这里的发送邮件的方法是带有相应的附件的,只是做了本地测试,所有相应的邮箱地址都写在了代码里面,这里可以根据需要自己灵活封装读取做变动),具体代码如下:
package appsoft.cd.applications.cd_gcxm.web;
import java.util.Properties;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class SendToEmils {
public static void main(String[] args) throws Exception { // 配置信息
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.163.com");
properties.put("mail.smtp.auth", "true");
// 根据邮件的会话属性构造一个发送邮件的Session,
//这里需要注意的是用户名那里不能加后缀,否则便不是用户名了
//还需要注意的是,这里的密码不是正常使用邮箱的登陆密码,而是客户端生成的另一个专门的授权码
MailAuthenticator authenticator = new MailAuthenticator("邮箱地址", "授权码");
Session session = Session.getInstance(properties, authenticator);
// 根据Session 构建邮件信息
Message message = new MimeMessage(session);
// 创建邮件发送者地址
Address from = new InternetAddress("邮箱地址");
// 设置邮件消息的发送者
message.setFrom(from);
// 验证收件人邮箱地址
List<String> toAddressList = new ArrayList<String>();
toAddressList.add("发送的邮箱地址");
StringBuffer buffer = new StringBuffer();
if (!toAddressList.isEmpty())
{ String regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern p = Pattern.compile(regEx);
for (int i = 0; i < toAddressList.size(); i++) {
Matcher match = p.matcher(toAddressList.get(i));
if (match.matches()) {
buffer.append(toAddressList.get(i));
if (i < toAddressList.size() - 1) {
buffer.append(",");
}
}
}
}
String toAddress = buffer.toString();
if (!toAddress.isEmpty()) {
// 创建邮件的接收者地址
Address[] to = InternetAddress.parse(toAddress);
// 设置邮件接收人地址
message.setRecipients(Message.RecipientType.TO, to);
// 邮件主题
message.setSubject("这是测试的邮件");
// 邮件容器
MimeMultipart mimeMultiPart = new MimeMultipart();
// 设置HTML
BodyPart bodyPart = new MimeBodyPart();
// 邮件内容
String htmlText = "测试的邮件发送成功了";
bodyPart.setContent(htmlText, "text/html;charset=utf-8");
mimeMultiPart.addBodyPart(bodyPart);
// 添加附件
List<String> fileAddressList = new ArrayList<String>();
fileAddressList.add("E:\\Test\\附件文件名称");
if (fileAddressList != null) {
BodyPart attchPart = null;
for (int i = 0; i < fileAddressList.size(); i++) {
if (!fileAddressList.get(i).isEmpty()) {
attchPart = new MimeBodyPart();
// 附件数据源
FileDataSource source = new FileDataSource(
fileAddressList.get(i));
// 将附件数据源添加到邮件体
attchPart.setDataHandler(new DataHandler(source));
// 设置附件名称为原文件名
attchPart.setFileName(MimeUtility.encodeText(source.getName()));
mimeMultiPart.addBodyPart(attchPart);
}
}
}
message.setContent(mimeMultiPart);
message.setSentDate(new Date());
// 保存邮件
message.saveChanges();
// 发送邮件
Transport.send(message);
}
}
}
测试邮件发送成功,这里我是用163邮箱给QQ邮箱做的测试,测试是成功通过的,可以使用自己的qq邮箱申请授权码进行测试